package com.zjp.mg.service;

import com.mongodb.client.result.UpdateResult;
import com.zjp.mg.dao.AbstractMongoService;
import com.zjp.mg.model.Order;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class OrderService extends AbstractMongoService<Order> {
    @Override
    public String getCollectionName() {
        return "order";
    }


    @Override
    public Class<Order> getType() {
        return Order.class;
    }


    public Order insertByAutoId(Order object) {
        object.setOrderId(getNextAutoId().toString());
        return insert(object);
    }


    public long updateById(String orderId, BigDecimal orderPrice) {
        UpdateResult updateResult = getMongoTemplate().
                updateMulti(new Query(Criteria.where("id").is(orderId)),
                        new Update().set("orderPrice", orderPrice), getType());
        return updateResult.getModifiedCount();
    }


}
